package com.gccloud.design;

import com.alibaba.fastjson.JSONObject;
import com.gccloud.design.entity.StfAccessManagementEntity;
import com.gccloud.design.feign.BwpOperDockingClient;
import com.gccloud.design.service.IStfAccessManagementService;
import com.gccloud.starter.common.exception.GlobalException;
import com.gccloud.starter.common.vo.R;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : StfAccessManagementController
 * @Author :
 * @Date : 2020/12/23 09:49
 * @Description :
 */

@RestController
@Api(tags = "业务场景目录管理")
@RequestMapping("/am")
public class StfAccessManagementController {

    @Resource
    private IStfAccessManagementService iStfAccessManagementService;
    @Resource
    private BwpOperDockingClient bwpOperDockingClient;


    @PostMapping("/getZtreeNodes")
    @ApiOperation(value = "查询业务场景目录", notes = "查询业务场景目录", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父节点id,根节点默认给-2", paramType = "query",  dataType = "string"),
            @ApiImplicitParam(name = "nodeId", value = "节点id", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "nodeType", value = "节点类型", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "leafNode", value = "是否叶子节点", paramType = "query", dataType = "string")
    })
    public R<List> getZtreeNodes(@ApiIgnore @RequestBody Map<String, String> params) {
        List list = iStfAccessManagementService.getZtreeNodes(params);
        return R.success(list);
    }

    /**
     * 新增注册*/

    @PostMapping("/add")
    @ApiOperation(value = "新增注册", notes = "新增注册", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<String> add(@ApiParam(name = "新增注册对象", value = "传入json格式", required = true) @RequestBody StfAccessManagementEntity stfAccessManagementEntity) {
        iStfAccessManagementService.add(stfAccessManagementEntity);
        return R.success(stfAccessManagementEntity.getId());
    }

    /**
     * 修改注册
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改注册", notes = "修改注册", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> update(@ApiParam(name = "修改注册", value = "传入json格式", required = true) @RequestBody StfAccessManagementEntity stfAccessManagementEntity) {
        iStfAccessManagementService.update(stfAccessManagementEntity);
        return R.success();
    }

    /**
     * 根据id删除修改注册系统
     */
    @PostMapping("/deleteById/{id}")
    @ApiOperation(value = "删除注册系统", notes = "删除注册系统", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> deleteById(@ApiParam(name = "删除注册系统", value = "传入String格式", required = true) @PathVariable("id") String idStr) {
        List<String> ids = convert(idStr);
        for (String id : ids) {
            StfAccessManagementEntity entity = iStfAccessManagementService.getBaseDao().selectById(id);
            iStfAccessManagementService.getBaseDao().deleteByIdWithDp(id);
            // 同步更新流程平台stf_access_management表数据，先赋值主键
            bwpOperDockingClient.deleteSysInsert(entity.getTenantId());
        }
        return R.success();
    }

    public List<String> convert(String idStr) {
        if (StringUtils.isBlank(idStr)) {
            throw new GlobalException("id不允许为空");
        } else {
            String[] idStrArr = StringUtils.split(idStr, "-");
            return Arrays.stream(idStrArr).collect(Collectors.toList());
        }
    }

}
